package scheduletask;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import org.testng.util.TimeUtils;
import scheduletask.Case1.LampTask;

public class Executor1 {

    /**
     * 定时任务执行间隔，1，2，3等等
     */
    private static final int interval = 1;

    private static List<LampTask> INSTANCE = new ArrayList<>();

    private static ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(1,
        new ThreadFactoryBuilder().setNameFormat(" lamp-executor-%d").setDaemon(false).build());

//    private static Runnable r = () -> INSTANCE.forEach(task -> {
//
//        if (task.getCountdown() <= 0) {
//            // 可执行次数 -1
//            task.setCount(task.getCount() - interval);
//
//            if (task.getCount() != 0) {
//                task.setCountdown(task.getPeriod());
//            } else {
//                INSTANCE.remove(task);
//            }
//
//            // 发送跑马灯
//
//            // 更新倒计时
//        } else {
//            task.setCountdown(task.getCountdown() - interval);
//        }
//    });

//    private static ScheduledFuture<?> scheduledFuture = threadPoolExecutor.scheduleAtFixedRate(r, 0, 1, TimeUnit.SECONDS);

//    static class LampTask {
//
//        /**
//         * 跑马灯内容
//         */
//        private String context;
//
//        /**
//         * 总执行次数
//         */
//        private int count;
//
//        /**
//         * 执行间隔
//         */
//        private int period;
//
//        /**
//         * 距下一次执行的倒计时，单位秒
//         */
//        private int countdown;
//
//        public String getContext() {
//            return context;
//        }
//
//        public void setContext(String context) {
//            this.context = context;
//        }
//
//        public int getCount() {
//            return count;
//        }
//
//        public void setCount(int count) {
//            this.count = count;
//        }
//
//        public int getPeriod() {
//            return period;
//        }
//
//        public void setPeriod(int period) {
//            this.period = period;
//        }
//
//        public int getCountdown() {
//            return countdown;
//        }
//
//        public void setCountdown(int countdown) {
//            this.countdown = countdown;
//        }
//
//        @Override public String toString() {
//            return "LampTask{" + "context='" + context + '\'' + ", count=" + count + ", period=" + period
//                + ", countdown=" + countdown + '}';
//        }
//    }






    /**
     * 间隔，给定的值
     * 计算出次数
     *
     *
     *
     * 用测试只会执行1次
     */
    @Test
    private void t1() {

        Runnable runnable = () -> System.out.println("send");
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);

        service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
//        service.shutdown();
    }

    /**
     * 定时任务放在main方法中能够持续执行
     * @param args
     */
    public static void main(String[] args) {





        Runnable runnable = () -> System.out.println("task 1" + Thread.currentThread());
        Runnable runnable2 = () -> System.out.println("task 2" + Thread.currentThread());

        // 1个线程，但是多个任务
        // 用户线程和内核线程的切换，上下文切换

        // 如果设置为守护线程，不会在控制台输出内容
//        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1,
//            new ThreadFactoryBuilder().setNameFormat("lamp-executor-%d").setDaemon(true).build());

        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1,
            new ThreadFactoryBuilder().setNameFormat(" lamp-executor-%d").setDaemon(false).build());

//        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);

//        service.schedule(runnable2, 1, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);

        // 周期不能是0
//        service.scheduleAtFixedRate(runnable2, 0, 0, TimeUnit.SECONDS);


//        for (int i = 0; i < 9; i++) {
////            service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
//
//            // 延迟2秒后执行，
////            service.schedule(runnable, 2, TimeUnit.SECONDS);
//
//            service.scheduleWithFixedDelay(runnable, 0, 1, TimeUnit.SECONDS);
//        }
//        service.shutdown();


        /* 推送给所有人
           只有一个


         */
    }

    // 任务定时，

    // 定时任务用于管理任务队列，将任务从队列中取出，从任务队列中取任务


    private void addTask(String context, int period) {

        Runnable r = () -> System.out.println(context);

        // 这是在每次都会创建一个定时器，所以创建定时器不能写在方法中
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1,
            new ThreadFactoryBuilder().setNameFormat(" lamp-executor-%d").setDaemon(false).build());

        executor.scheduleAtFixedRate(r, 1, period, TimeUnit.SECONDS);


        executor.shutdown();

        // 取出来，就执行一次
//        executor.schedule();

    }

    @Test
    private void t2() {
        // 定时器用了这样的轮询 ThreadPoolExecutor.addWorker
        for (;;) {
            System.out.println("0000");
        }
    }



    static {
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();



    }



}
